package se.chalmers.tda366.crumble.gui.dialogs;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;

import se.chalmers.tda366.crumble.core.ICategory;
import se.chalmers.tda366.crumble.gui.utils.CloseActionListener;
import se.chalmers.tda366.crumble.gui.utils.GuiObserv;
import se.chalmers.tda366.crumble.resources.Constants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AddFeedView extends JPanel{
	
	private URL address; 
	private List<ICategory> categories;
	
	public AddFeedView(List<ICategory> list){
		categories=list;
		initGUI();
	}
	
	public String getFeedName() {
		return feedNameTextField.getText();
	}
	
	public ICategory getCategory() {
		return (ICategory) categoryComboBox.getSelectedItem();
	}
	
	public URL getFeedURL() {
		return address;
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(428, 221));
			this.setSize(428, 221);
			{
				addFeedPanel = new JPanel();
			}
			{
				HeaderLabel = new JLabel();
				HeaderLabel.setFont(se.chalmers.tda366.crumble.resources.Constants.HEADLINEFONT);
				HeaderLabel.setText("Add a new Subscription...");
				HeaderLabel.setHorizontalTextPosition(SwingConstants.CENTER);
				HeaderLabel.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				addButton = new JButton();
				addButton.setText("Add feed");
				addButton.setToolTipText("Add feed");
				addButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						try {
							address = new URL(feedAdressTextField.getText());
							GuiObserv.getInstance().fireNotify(((JButton)evt.getSource()).getParent(), Constants.Actions.ADD_FEED);
						} catch (MalformedURLException e1) {
							JOptionPane.showMessageDialog(null, "Feed adress is invalid");
						}
					}
				});
			}
			{
				exitButton = new JButton();
				exitButton.setText("Cancel");
				exitButton.addActionListener(new CloseActionListener());
			}
			{
				ComboBoxModel CategoryComboBoxModel = 
					new DefaultComboBoxModel(categories.toArray());
				categoryComboBox = new JComboBox();
				categoryComboBox.setEditable(false);
				categoryComboBox.setModel(CategoryComboBoxModel);
				categoryComboBox.setNextFocusableComponent(addButton);
			}
			{
				feedNameLabel = new JLabel();
				feedNameLabel.setText("Feed Name:");
				feedNameLabel.setAlignmentX(-0.0f);
			}
			{
				feedNameTextField = new JTextField();
			}
			{
				addressLabel = new JLabel();
				addressLabel.setText("Feed Address:");
			}
			{
				feedAdressTextField = new JTextField();
				feedNameTextField.setNextFocusableComponent(feedAdressTextField);
				feedAdressTextField.setNextFocusableComponent(categoryComboBox);
			}
			{
				feedCategoryLabel = new JLabel();
				feedCategoryLabel.setText("Feed Category:");
			}
			{
				categoryScrollBar = new JScrollBar();
			}
				thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addGap(7)
					.addGroup(thisLayout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					        .addComponent(addFeedPanel, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
					        .addGap(17)
					        .addComponent(categoryScrollBar, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
					        .addGap(28))
					    .addComponent(HeaderLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(feedNameTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(feedNameLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(feedAdressTextField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(addressLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(categoryComboBox, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(feedCategoryLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(addButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(exitButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(141, 141));
				thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addComponent(categoryScrollBar, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
					.addGap(12)
					.addComponent(addFeedPanel, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
					.addGap(46)
					.addGroup(thisLayout.createParallelGroup()
					    .addGroup(thisLayout.createSequentialGroup()
					        .addGroup(thisLayout.createParallelGroup()
					            .addComponent(feedCategoryLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					            .addComponent(addressLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					        .addGroup(thisLayout.createParallelGroup()
					            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					                .addComponent(categoryComboBox, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 36, Short.MAX_VALUE))
					            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					                .addComponent(feedAdressTextField, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 36, Short.MAX_VALUE))
					            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					                .addComponent(feedNameTextField, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 36, Short.MAX_VALUE))
					            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					                .addGap(66)
					                .addComponent(exitButton, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
					                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					                .addComponent(addButton, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 0, Short.MAX_VALUE))))
					    .addGroup(thisLayout.createSequentialGroup()
					        .addPreferredGap(feedCategoryLabel, feedNameLabel, LayoutStyle.ComponentPlacement.INDENT)
					        .addGroup(thisLayout.createParallelGroup()
					            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					                .addComponent(feedNameLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 251, Short.MAX_VALUE))
					            .addGroup(thisLayout.createSequentialGroup()
					                .addComponent(HeaderLabel, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 0, Short.MAX_VALUE)))))
					.addContainerGap(25, 25));
				thisLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {feedNameTextField, feedAdressTextField, categoryComboBox});
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.repaint();
	}
	private JPanel addFeedPanel;
	private JLabel feedNameLabel;
	private JLabel HeaderLabel;
	private JButton exitButton;
	private JButton addButton;
	private JComboBox categoryComboBox;
	private JScrollBar categoryScrollBar;
	private JLabel feedCategoryLabel;
	private JTextField feedAdressTextField;
	private JLabel addressLabel;
	private JTextField feedNameTextField;

}
